/// <reference path="jquery.js"/>

(function($) {
	$.extend({

		/***********************************************************************
		 * jQuery.codeplex
		 *  - getDefaultVersion : gets the default version for the specified project
		 **********************************************************************/
		codeplex:{
			getDefaultVersion: function(proj) {
				var html = $.ajax({
					url   : 'http://'+proj+'.codeplex.com/Release/ProjectReleases.aspx',
					async : false,
					cache : false
				}).responseText;
				var match = html.match(/title="Recommended Release: [0-9]+.[0-9]+(.[0-9]+(.[0-9]+)?)?"/gm);
				if (match.length == 0) { return null; }
				var ver = match[0];
				if (ver) { ver = ver.substring(28, ver.length-1); }
				return ver;
			}
		},


		/***********************************************************************
		 * jQuery.date
		 *  - toString     : returns date as "Mon dd, yyyy"
		 *  - toTimeString : returns time as "hh:mi:ss AM"
		 **********************************************************************/
		date:{
			toString: function(date) {
				if (date == null) { return null; }
				var months = new Array();
				months[0] = "Jan"; months[1] = "Feb"; months[2] = "Mar"; months[3] = "Apr";
				months[4] = "May"; months[5] = "Jun"; months[6] = "Jul"; months[7] = "Aug";
				months[8] = "Sep"; months[9] = "Oct"; months[10] = "Nov"; months[11] = "Dec";
				return months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
			},
			toTimeString: function(date) {
				if (date == null) { return null; }
				var isMorning = (date.getHours() < 11);
				return (isMorning ? (date.getHours() == 0 ? 12 : date.getHours()) : date.getHours() - 12)
					+ ":" + $.string.padLeft(date.getMinutes(), 0, 2)
					+ ":" + $.string.padLeft(date.getSeconds(), 0, 2)
					+ (isMorning ? " am" : " pm");
			}
		},


		/***********************************************************************
		 * jQuery.string
		 *  - format  : formats a string
		 *  - isEmpty : determines if string is null/empty
		 *  - padLeft : pads the specified 
		 **********************************************************************/
		string:{
			endsWith: function(str, val){
				if (!str || !val) { return false; }
				return (str.substring(str.length - val.length) == val);
			},
			format: function(str, args) {
				$.each(args, function(i, value) {
					str = str.replace(new RegExp('\\{' + i + '\\}', 'g'), value);
				});
				return str;
			},
			isEmpty: function(str){return (str==null || str=='');},
			padLeft: function(str, chr, len) {
				var s = new String(str);
				if (s == null) { s = ""; }
				for (var i = 0; i < len - s.length; i++) { s = chr + s; }
				return s;
			}
		},

	
		/***************************************************************************
		 * jQuery.version
		 *  - isNewer : determines if one version number is newer than another
		 **************************************************************************/
		version:
		{
			isNewer: function(current, latest){
				if ($.string.isEmpty(current) || $.string.isEmpty(latest)) { return false; }
				latest = latest.split('.');
				current = current.split('.');
				for (var i=0; i<4; i++){
					var l = (latest.length > i) ? parseInt(latest[i]) : 0;
					var c = (current.length > i) ? parseInt(current[i]) : 0;
					if (l > c) { return true; } else if (l < c) { return false; }
				}
				return false;
			}
		},
		
		
		def    : function(o) { return !$.undef(o); },
		undef  : function(o) { return (o == null || typeof(0) == 'undefined'); },
		ifNull : function(o, ifNull) { return $.def(o) ? o : ifNull; }
		
	});
})(jQuery);
